/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ControlInscripciones.java
 *
 * Created on 02-may-2012, 14:49:07
 */
package client.subsistema2;

import beans.ControlInscripcionsDTO;
import beans.Constants;
import common.EOlympicsException;
import common.RemoteInterface;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Daniel
 */
public class ControlInscripcions extends javax.swing.JFrame {

    /** Creates new form ControlInscripciones */
    public ControlInscripcions(RemoteInterface pRemoto, int usuari) {
        initComponents();
        codiUsuari=usuari;
        remoto=pRemoto;
        // quan sintegri llevar això i comentar al main()
        /*
        try {

                    remoto=(RemoteInterface)Naming.lookup(url);
        } catch (NotBoundException ex) {
            Logger.getLogger(AltaProves.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(AltaProves.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(AltaProves.class.getName()).log(Level.SEVERE, null, ex);
        }
        */
        // fi de llevar
    }
    int codiUsuari;
    RemoteInterface remoto;
    java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("i18n/subsistema2/messagesSubsistema2"); // NOI18N

        // llevar aixo en integrar
    private final String url = new String("rmi://localhost/RemoteInterface");
    // fi de llevar



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Prova", "Inscribibles", "Inscrits", "Estat"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.String.class
            };
            /*
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
            */
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Tancament inscripcions");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jMenu1.setText("Arxiu");

        jMenuItem1.setText("Cercar");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuItem2.setText("Tancar inscipcions");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuItem3.setText("Sortir");
        jMenu1.add(jMenuItem3);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Ayuda");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 426, Short.MAX_VALUE)
                        .addComponent(jButton2))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 638, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(79, 79, 79)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    /*
     * Aquest mètode cirda al client per fer el tancamen
     * només haurà de menester 2 paràmetres, l'usuari i el mínim per poder declarar la prova deserta
     */
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            int minim=Constants.NUM_MIN_PARTICIPANTS_PROVA;
            remoto.sub2tancaInscripcions(codiUsuari, minim);
            jButton1ActionPerformed(evt);
        } catch (RemoteException ex) {
            System.out.println("Problmes tancant les inscripcions");
        }catch (EOlympicsException e) {
               System.out.print("EOlympicsException");
               System.out.println(bundle.getString("client.tanca_inscipcions"));
       }
}//GEN-LAST:event_jButton2ActionPerformed


/*
 * aquest mètode fa la càrrega, i la simulació del que es farà si es tanca
 */
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            List<ControlInscripcionsDTO> lista=remoto.sub2mostraControlInscripcions();
            jTable1.removeAll();
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("Prova");
            model.addColumn("Inscribibles");
            model.addColumn("Inscrits");
            model.addColumn("Estat");
            int minim=Constants.NUM_MIN_PARTICIPANTS_PROVA;
            for(ControlInscripcionsDTO ci: lista)
            {
                String estat;
                if(ci.getInscrits().intValue()<minim)
                    estat="Deserta";
                else
                    if (ci.getInscrits().intValue()>Constants.NUM_CLASSIFICATS_RONDA_FINAL)
                        estat="En competició prèvia";
                    else
                        estat="En competició final";
                String[] cols = {ci.getNom(),ci.getNombreFinalistes().toString(),ci.getInscrits().toString(),estat};
                model.addRow(cols);                
            }
            jTable1.setModel(model);
        } catch (RemoteException ex) {
            System.out.println("Problemes mostrant les inscripcions");
        }catch (EOlympicsException e) {
               System.out.print("EOlympicsException");
               System.out.println(bundle.getString("client.carregamt_inscipcions"));
       }

}//GEN-LAST:event_jButton1ActionPerformed

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed

    jButton1ActionPerformed(evt);
}//GEN-LAST:event_jMenuItem1ActionPerformed

private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed

    jButton2ActionPerformed(evt);
}//GEN-LAST:event_jMenuItem2ActionPerformed






    /*
    public static void main(String args[]) {

        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ControlInscripcions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ControlInscripcions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ControlInscripcions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ControlInscripcions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ControlInscripcions(2).setVisible(true);
            }
        });
    }

    */


// Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
